#ifdef MBM_IMIBASE_SUPPORT

#include "mbm_mtk_inc.h"
#include "MMIDataType.h"
#include "gdi_datatype.h"
#include "MMI_features.h"//for mdi_camera.h, or :(
#include "mdi_datatype.h"
#include "mdi_camera.h"
#include "iul_color_convert.h"

#include "mbm_typedefine.h"
#include "mbm_camera_adapter.h"
#include "mbm_sys_adapter.h"
#include "mbm_memory_adapter.h"
#include "mbm_config.h"


#if defined(MBM_DEBUG_CAM)
#define CAM_TRACE mbm_trace
#else
#define CAM_TRACE(...) 
#endif

#define __MBM_CAM_HORIZONTAL_SENSOR__
#define __MBM_CAM_ROTATE_180__

#define MBM_CAMERA_WAIT_EVENT(evt_) do{                           \
         UInt32 retrieved_events;                             \
         CAM_TRACE("wait evt: %s", #evt_);                 \
         kal_retrieve_eg_events(g_mbm_camera_context_p->cam_event, (evt_), \
         KAL_OR_CONSUME, &retrieved_events, KAL_SUSPEND); }while(0)

#define MBM_CAMERA_SET_EVENT(evt_) do{                             \
         kal_set_eg_events(g_mbm_camera_context_p->cam_event, (evt_),KAL_OR);\
         CAM_TRACE("set evt: %s", #evt_);\
         } while(0)


typedef enum
{    
    MBM_CAMERA_CAPTURE_YUV420,
    MBM_CAMERA_CAPTURE_RGB565,
    MBM_CAMERA_CAPTURE_FORMAT_MAX
}e_mbm_camera_capture_format;

typedef enum
{
    MBM_CAMERA_PREVIEW_ROTATE_NORMAL,
    MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_90,
    MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_180,
    MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_270,
    MBM_CAMERA_PREVIEW_ROTATE_MAX
}e_mbm_camera_preview_rotate;

typedef enum
{
    MBM_CAMERA_EVT_NOWAIT = 0,
    MBM_CAMERA_EVT_POWER_ON = 0x0001,
    MBM_CAMERA_EVT_INIT = 0x0002,
    MBM_CAMERA_EVT_CAPTURE = 0x0004,
    MBM_CAMERA_EVT_DEINIT = 0x008,
    MBM_CAMERA_EVT_POWER_OFF = 0x00010,
    MBM_CAMERA_EVT_MAX = 0x00020
}e_mbm_camera_wait_event;

typedef struct
{
    U16 image_width;                    /* Capture image width */
    U16 image_height;                   /* Capture image height */
    U16 preview_width;                  /* Preview image width */
    U16 preview_height;                 /* Preview image height */
    U16 lcm;                            /* LCM */
    U16 preview_rotate;                 /* Preview rotation */
    U16 lcm_rotate;                     /* LCM rotation */
    U8 continue_capture;                /* Continue capture flag */
    void (*xenon_flash_status_callback) (mdi_result);   /* xenon flash status callback function pointer */

    gdi_handle preview_layer_handle;    /* Preview layer handle */
    S32 preview_wnd_offset_x;           /* Preview window offset x */
    S32 preview_wnd_offset_y;           /* Preview window offset y */
    U16 preview_wnd_width;              /* Preview window width */
    U16 preview_wnd_height;             /* Preview window height */
    U32 blt_layer_flag;                 /* Layer flags of needing to blt when camera previewing */
    U32 preview_layer_flag;             /* Previwe layer flag */
    BOOL is_lcd_update;                 /* LCD update the preview layer flag */
    U16 src_key_color;                  /* Source key color */
    BOOL is_tvout;                      /* Update to TV flag */ 
}t_mbm_camera_settting;

typedef struct
{
    UInt8 state;
    UInt8 src_mod;//msg send in which task, e,g, MOD_MMI
    UInt8 cam_mod;//msg process in which task, e.g. MOD_MED
    
    UInt8 preview_rotate;
    UInt16 width;
    UInt16 height;
    UInt32 rgb_buff_addr;
    UInt32 buff_addr2;//for rotate
    UInt16 format;
    UInt32 callback;
    UInt8* yuv420_data_buffer;

    kal_eventgrpid cam_event;    
    Int32 cam_capture_mem;
}t_mbm_camera_context;

static t_mbm_camera_context g_mbm_camera_context = {0};
static t_mbm_camera_context* g_mbm_camera_context_p = &g_mbm_camera_context;

static t_mbm_camera_settting g_mbm_camera_setting;                                                                        
static mdi_camera_setting_struct g_camera_setting_data;
static mdi_camera_preview_struct g_camera_preview_data;

/******************************************************************************/
/*
 * mbm camera module init
 */

static Int32 mbm_camera_init(void);

/*
 * mbm camera API for MMI
 */
static Int32 mbm_camera_power_on(void);
static Int32 mbm_camera_preview(UInt16 width, UInt16 height);
static Int32 mbm_camera_data_req(void);
static Int32 mbm_camera_preview_stop(void);
static Int32 mbm_camera_power_off(void);

extern int iul_rgb565_to_yuv420_non_interleave(unsigned short *srcRGB, 
                                        unsigned char *dstY, unsigned char *dstU, unsigned char *dstV, 
                                        int width, int height);


/***********************************resizer override start*****************************/

#define OVERRIDE(func) $Sub$$##func
#define REAL(func) $Super$$##func

#define MBM_RESZ_INT_STATUS             0x8401000c
#define MBM_RESZ_RGB_BUF1               0x84010084
#define MBM_RESZ_RGB_BUF2               0x84010088

//RESZ_INT_STATUS
#define MBM_RESZ_INT_LCD_BIT            0x0001
#define MBM_RESZ_INT_MEMO1_BIT          0x0002
#define MBM_RESZ_INT_FSTART_BIT         0x0004
#define MBM_RESZ_INT_PXDINT_BIT         0x0008
#define MBM_RESZ_INT_MEMO2_BIT          0x0010

//intctrl_6253.h
#define MBM_IRQ_RESZ_CODE               (21) /*spec : RESIZER*/

#define MBM_RESIZER_DATA_OUTPUT_ENABLE() { g_mbm_resizer_misc.pts_req = IMP_True; }
#define MBM_RESIZER_DATA_OUTPUT_DISABLE() { g_mbm_resizer_misc.pts_req = IMP_False; }
#define MBM_RESIZER_ENABLE() { g_mbm_resizer_misc.pts_start = IMP_True; }
#define MBM_RESIZER_DISABLE() { g_mbm_resizer_misc.pts_start = IMP_False; }


/******************************************************************************/
typedef struct
{
    UInt8 pts_start;
    UInt8 pts_req; 
}t_mbm_resizer_misc;

static t_mbm_resizer_misc g_mbm_resizer_misc;

extern void IRQUnmask(UInt8 code);

/******************************************************************************/

static void mbm_camera_resizer_callback(UInt8 return_code)
{
    UInt32 buf_addr;
    UInt32 buf_len;

    //CAM_TRACE("enter %s", __func__); 

	//FIXME?
	buf_addr = DRV_Reg32(MBM_RESZ_RGB_BUF2);   
	buf_len = g_mbm_camera_context_p->width * g_mbm_camera_context_p->height * 2;

	kal_mem_cpy((void*)(g_mbm_camera_context_p->rgb_buff_addr), (void*)buf_addr, buf_len);

    MBM_RESIZER_DATA_OUTPUT_DISABLE();
    
	MBM_CAMERA_SET_EVENT(MBM_CAMERA_EVT_CAPTURE);
}

void OVERRIDE(RESZ_HISR)(void)
{
    UInt32 reg;
    
    if(!g_mbm_resizer_misc.pts_start)
    {
        REAL(RESZ_HISR)();
        return;
    }

    reg = DRV_Reg32(MBM_RESZ_INT_STATUS);

    //CAM_TRACE("enter %s 0x%08x", __func__, reg);

    if (reg & MBM_RESZ_INT_MEMO1_BIT)
    {        
        //CAM_TRACE("enter %s RESZ_INT_MEMO1_BIT", __func__);
		if(g_mbm_resizer_misc.pts_req)
        {
            mbm_camera_resizer_callback(0);
            g_mbm_resizer_misc.pts_req = 0;
        }		
    }

    #if 0
    if (reg & MBM_RESZ_INT_LCD_BIT)
    {    
        //CAM_TRACE("enter %s RESZ_INT_LCD_BIT", __func__);
    }
    
    if (reg & MBM_RESZ_INT_FSTART_BIT)
    {
        //CAM_TRACE("enter %s RESZ_INT_FSTART_BIT", __func__);        
    }
    
    if (reg & MBM_RESZ_INT_PXDINT_BIT)
    {        
        //CAM_TRACE("enter %s RESZ_INT_PXDINT_BIT", __func__);
    }
    
    if (reg & MBM_RESZ_INT_MEMO2_BIT)
    {
        //CAM_TRACE("enter %s RESZ_INT_MEMO2_BIT", __func__);
    }
    #endif

    IRQUnmask(MBM_IRQ_RESZ_CODE);
}

/*********************************resizer override end****************************/


void mbm_camera_set_setting(t_mbm_camera_settting* mbm_camera_setting, UInt16 width, UInt16 height)
{
	mbm_camera_setting->lcm = 0;//invalid lcd
	mbm_camera_setting->preview_width = width;
	mbm_camera_setting->preview_height = height;

	mbm_camera_setting->preview_rotate = 0;//CAM_IMAGE_NORMAL = 0,
	mbm_camera_setting->lcm_rotate = 0;//LCD_LAYER_ROTATE_NORMAL
	mbm_camera_setting->image_width = width;
	mbm_camera_setting->image_height = height;
	mbm_camera_setting->continue_capture = 0;//?
	mbm_camera_setting->xenon_flash_status_callback = NULL;
	
	/* preview data for preview start */
	mbm_camera_setting->preview_layer_handle = 2;
	mbm_camera_setting->preview_wnd_offset_x = 0;
	mbm_camera_setting->preview_wnd_offset_y = 0;
	mbm_camera_setting->preview_wnd_width = width;
	mbm_camera_setting->preview_wnd_height = height;
 
	mbm_camera_setting->blt_layer_flag =  0x80000000 | 0x40000000;//GDI_LAYER_ENABLE_LAYER_0 | GDI_LAYER_ENABLE_LAYER_1;
	mbm_camera_setting->preview_layer_flag =  0x40000000;//GDI_LAYER_ENABLE_LAYER_1;
	mbm_camera_setting->is_lcd_update = 0;
	mbm_camera_setting->src_key_color = 0;/* this value is use only CAMERA_MODULE_WITH_LCD */
	mbm_camera_setting->is_tvout = 0;    
}

static void mbm_camera_load_setting(mdi_camera_setting_struct* camera_setting_data, mdi_camera_preview_struct* camera_preview_data, t_mbm_camera_settting* mbm_camera_setting)
{
    camera_setting_data->lcm                          = mbm_camera_setting->lcm;
    camera_setting_data->preview_width                = mbm_camera_setting->preview_width;
    camera_setting_data->preview_height               = mbm_camera_setting->preview_height;
    
    camera_setting_data->preview_rotate               = mbm_camera_setting->preview_rotate;
    camera_setting_data->lcm_rotate                   = mbm_camera_setting->lcm_rotate;
    camera_setting_data->image_width                  = mbm_camera_setting->image_width;
    camera_setting_data->image_height                 = mbm_camera_setting->image_height;
    camera_setting_data->continue_capture             = mbm_camera_setting->continue_capture;
    camera_setting_data->xenon_flash_status_callback  = mbm_camera_setting->xenon_flash_status_callback;
    
    camera_preview_data->preview_layer_handle         = mbm_camera_setting->preview_layer_handle;
    camera_preview_data->preview_wnd_offset_x         = mbm_camera_setting->preview_wnd_offset_x;
    camera_preview_data->preview_wnd_offset_y         = mbm_camera_setting->preview_wnd_offset_y;
    camera_preview_data->preview_wnd_width            = mbm_camera_setting->preview_wnd_width;
    camera_preview_data->preview_wnd_height           = mbm_camera_setting->preview_wnd_height;
    
    camera_preview_data->blt_layer_flag               = mbm_camera_setting->blt_layer_flag;
    camera_preview_data->preview_layer_flag           = mbm_camera_setting->preview_layer_flag;
    camera_preview_data->is_lcd_update                = mbm_camera_setting->is_lcd_update;
    camera_preview_data->src_key_color                = mbm_camera_setting->src_key_color;
    camera_preview_data->is_tvout                     = mbm_camera_setting->is_tvout;
}

#ifdef __MBM_CAM_HORIZONTAL_SENSOR__
#define MIIAUX_CALC_OFFSET_YUV420_U(x_, y_, width_) ((y_ >> 1) * (width_ >> 1) + (x_ >> 1))
static void mbm_media_aux_yuv420_rotate_270(int src_width, int src_height, unsigned char *src_buffer, unsigned char *dst_buffer)
{
    int x, y;
    int pixel_count;
    unsigned char *src_u_part, *src_v_part;
    unsigned char *dst_u_part, *dst_v_part;

    if (NULL == dst_buffer)
    {
        return;
    }

    pixel_count = src_width * src_height;

    src_u_part = src_buffer + pixel_count;
    src_v_part = src_u_part + pixel_count / 4;

    dst_u_part = dst_buffer + pixel_count;
    dst_v_part = dst_u_part + pixel_count / 4;

    for (x = 0; x < src_height; x++)
    {
        for (y = 0; y < src_width; y++)
        {
            int y_pos;
            int src_y_pos;
            int src_x, src_y;

            src_x = y;
            src_y = src_height - 1 - x;

            y_pos = y * src_height + x;
            src_y_pos = src_y * src_width + src_x;
            dst_buffer[y_pos] = src_buffer[src_y_pos];

            if (!((x & 0x1) || (y & 0x1)))
            {
                int u_pos, v_pos;
                int src_u_pos, src_v_pos;
                u_pos = MIIAUX_CALC_OFFSET_YUV420_U(x, y, src_height);
                v_pos = u_pos;

                src_u_pos = MIIAUX_CALC_OFFSET_YUV420_U(src_x, src_y, src_width);
                src_v_pos = src_u_pos;

                dst_u_part[u_pos] = src_u_part[src_u_pos];
                dst_v_part[v_pos] = src_v_part[src_v_pos];
            }
        }
    }

}
#endif // __MBM_CAM_HORIZONTAL_SENSOR__

#if defined(__MBM_CAM_ROTATE_180__)

#define MIIAUX_CALC_OFFSET_YUV420_U_180(x_, y_, width_) ((x_ >> 1) * (width_ >> 1) + (y_ >> 1))

void mbm_media_aux_yuv42_rotate_180(int src_width, int src_height, unsigned char *src_buffer, unsigned char *dst_buffer)
{
    int x, y;
    int pixel_count;
    unsigned char *src_u_part, *src_v_part;
    unsigned char *dst_u_part, *dst_v_part;

    if (NULL == dst_buffer)
    {
        return;
    }

    pixel_count = src_width * src_height;

    src_u_part = src_buffer + pixel_count;
    src_v_part = src_u_part + pixel_count / 4;

    dst_u_part = dst_buffer + pixel_count;
    dst_v_part = dst_u_part + pixel_count / 4;

    for (x = 0; x < src_height; x++)
    {
        for (y = 0; y < src_width; y++)
        {
            int y_pos;
            int src_y_pos;
            int src_x, src_y;

            y_pos = x * src_width + y;
            src_y_pos = (src_height - 1 - x)*src_width+(src_width-1-y);

            dst_buffer[y_pos] = src_buffer[src_y_pos];

            if (!((x & 0x1) || (y & 0x1)))
            {
                int u_pos, v_pos;
                int src_u_pos, src_v_pos;
                u_pos = MIIAUX_CALC_OFFSET_YUV420_U_180(x, y, src_width);
                v_pos = u_pos;

                src_u_pos = MIIAUX_CALC_OFFSET_YUV420_U_180((src_height - 1 - x), (src_width-1-y), src_width);
                src_v_pos = src_u_pos;

                dst_u_part[u_pos] = src_u_part[src_u_pos];
                dst_v_part[v_pos] = src_v_part[src_v_pos];
            }
        }
    }
}

#define MIIAUX_CALC_OFFSET_YUV420_U_90(x_, y_, width_) ((y_ >> 1) * (width_ >> 1) + (x_ >> 1))

void mbm_media_aux_yuv42_rotate_90(int src_width, int src_height, unsigned char *src_buffer, unsigned char *dst_buffer)
{
    int x, y;
    int pixel_count;
    unsigned char *src_u_part, *src_v_part;
    unsigned char *dst_u_part, *dst_v_part;

    if (NULL == dst_buffer)
    {
        return;
    }

    pixel_count = src_width * src_height;

    src_u_part = src_buffer + pixel_count;
    src_v_part = src_u_part + pixel_count / 4;

    dst_u_part = dst_buffer + pixel_count;
    dst_v_part = dst_u_part + pixel_count / 4;

    for (x = 0; x < src_height; x++)
    {
        for (y = 0; y < src_width; y++)
        {
            int y_pos;
            int src_y_pos;
            int src_x, src_y;

            src_x = src_width - 1 - y;
            src_y = x;

            y_pos = y * src_height + x;
            src_y_pos = src_y * src_width + src_x;
            dst_buffer[y_pos] = src_buffer[src_y_pos];

            if (!((x & 0x1) || (y & 0x1)))
            {
                int u_pos, v_pos;
                int src_u_pos, src_v_pos;
                u_pos = MIIAUX_CALC_OFFSET_YUV420_U(x, y, src_height);
                v_pos = u_pos;

                src_u_pos = MIIAUX_CALC_OFFSET_YUV420_U(src_x, src_y, src_width);
                src_v_pos = src_u_pos;

                dst_u_part[u_pos] = src_u_part[src_u_pos];
                dst_v_part[v_pos] = src_v_part[src_v_pos];
            }
        }
    }
}
#endif

Int32 mbm_camera_init(void)
{
    g_mbm_camera_context_p->src_mod = MOD_MMI;
    g_mbm_camera_context_p->cam_mod = MOD_MED;
    if(0 == g_mbm_camera_context_p->cam_event)
    {
    	g_mbm_camera_context_p->cam_event = kal_create_event_group("mbm_cam_events");
    }
    g_mbm_camera_context_p->preview_rotate = MBM_CAMERA_PREVIEW_ROTATE_NORMAL;
    //g_mbm_camera_context_p->cam_capture_mem =  g_miiplt_max_cam_cap_int_mem_size;//line buffer
    return MBM_CAMERA_OK;
}    

/**
  *  [PLT_REVIEW] modify return value
  */
Int32  mbm_camera_power_on(void)
{       
    MBM_RESIZER_ENABLE();
    
    if(MDI_RES_CAMERA_SUCCEED != mdi_camera_power_on())
    {
		return MBM_CAMERA_FAIL;
    }
    
	return MBM_CAMERA_OK;
}

Int32 mbm_camera_preview(UInt16 width, UInt16 height)
{       
     /**
       * [PLT_REVIEW] ensure return g_mbm_camera_context_p->cam_result. 
       */
    if((g_mbm_camera_context_p->preview_rotate == MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_90)
        ||(g_mbm_camera_context_p->preview_rotate == MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_270))
    {
		g_mbm_camera_context_p->width = height;
		g_mbm_camera_context_p->height = width;
    }
    else
    {        
		g_mbm_camera_context_p->width = width;
		g_mbm_camera_context_p->height = height;
    }
    
    g_mbm_camera_context_p->rgb_buff_addr = (UInt32) MBM_Malloc(width * height * 2);
    if(g_mbm_camera_context_p->preview_rotate != MBM_CAMERA_PREVIEW_ROTATE_NORMAL)
    {
        g_mbm_camera_context_p->buff_addr2 = (UInt32) MBM_Malloc(width * height * 3/2); 
    }
    
    g_mbm_camera_context_p->format = MBM_CAMERA_CAPTURE_YUV420;
    
	mdi_camera_load_default_setting(&g_camera_setting_data);

    mbm_camera_set_setting(&g_mbm_camera_setting, width, height);
    mbm_camera_load_setting(&g_camera_setting_data, &g_camera_preview_data, &g_mbm_camera_setting);

	if(MDI_RES_CAMERA_SUCCEED != mdi_camera_preview_start(
									&g_camera_preview_data,
									&g_camera_setting_data))
	{
		return MBM_CAMERA_OK;
	}
	else
	{
		return MBM_CAMERA_FAIL;
	}
}

Int32  mbm_camera_preview_stop(void)
{       
	Int32 ret = 0;
	
    g_mbm_camera_context_p->width = 0;
    g_mbm_camera_context_p->height = 0;
    if(g_mbm_camera_context_p->rgb_buff_addr)
    {
        MBM_Free((void*)g_mbm_camera_context_p->rgb_buff_addr);
        g_mbm_camera_context_p->rgb_buff_addr = 0;
    }
    if(g_mbm_camera_context_p->buff_addr2)
    {
        MBM_Free((void*)g_mbm_camera_context_p->buff_addr2);
        g_mbm_camera_context_p->buff_addr2 = 0;
    }

    g_mbm_camera_context_p->format = 0;
    g_mbm_camera_context_p->callback = 0;
    
    ret = (Int32)mdi_camera_preview_stop();
    
    MBM_RESIZER_DATA_OUTPUT_DISABLE();

	if(MDI_RES_CAMERA_SUCCEED == ret)
	{
		return MBM_CAMERA_OK;
	}
	else
	{
		return MBM_CAMERA_FAIL;
	}
}

Int32  mbm_camera_power_off(void)
{    
	Int32 ret = 0;
	
    ret = (Int32)mdi_camera_power_off();
    
    MBM_RESIZER_DISABLE();
    
	if(MDI_RES_CAMERA_SUCCEED == ret)
	{
		return MBM_CAMERA_OK;
	}
	else
	{
		return MBM_CAMERA_FAIL;
	}
}

Int32 mbm_camera_data_req(void)
{    
    UInt32 buf_addr;
    UInt32 buf_len;
    UInt32 plane;
    
    MBM_RESIZER_DATA_OUTPUT_ENABLE();
    
    MBM_CAMERA_WAIT_EVENT(MBM_CAMERA_EVT_CAPTURE);

    MBM_ASSERT(g_mbm_camera_context_p->yuv420_data_buffer != NULL, "mbm_camera_data_req");
    
	if(g_mbm_camera_context_p->format == MBM_CAMERA_CAPTURE_YUV420)
	{		  
		buf_addr = g_mbm_camera_context_p->rgb_buff_addr;	
		buf_len = g_mbm_camera_context_p->width * g_mbm_camera_context_p->height * 2;


		//convert rgb565 to yuv420
		plane= (UInt32)g_mbm_camera_context_p->width * g_mbm_camera_context_p->height; 
		iul_rgb565_to_yuv420_non_interleave(
		(unsigned short *)buf_addr,
		(unsigned char *)g_mbm_camera_context_p->yuv420_data_buffer,
		(unsigned char *)(g_mbm_camera_context_p->yuv420_data_buffer + plane),
		(unsigned char *)(g_mbm_camera_context_p->yuv420_data_buffer + plane*5/4),
		(int)g_mbm_camera_context_p->width, 
		(int)g_mbm_camera_context_p->height);
		
		//rotate
		if(g_mbm_camera_context_p->preview_rotate == MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_270)
		{
			CAM_TRACE("rotate anti-clock 270");
			mbm_media_aux_yuv420_rotate_270((int)g_mbm_camera_context_p->width, (int)g_mbm_camera_context_p->height, 
			(unsigned char *)g_mbm_camera_context_p->yuv420_data_buffer,
			(unsigned char *)g_mbm_camera_context_p->buff_addr2);
		}
	#ifdef __MBM_CAM_ROTATE_180__
		else if(g_mbm_camera_context_p->preview_rotate == MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_90)
		{			  
			CAM_TRACE(" rotate anti-clock 90");
			mbm_media_aux_yuv42_rotate_90((int)g_mbm_camera_context_p->width, (int)g_mbm_camera_context_p->height, 
			(unsigned char *)g_mbm_camera_context_p->yuv420_data_buffer,
			(unsigned char *)g_mbm_camera_context_p->buff_addr2);
		}
		else if(g_mbm_camera_context_p->preview_rotate == MBM_CAMERA_PREVIEW_ROTATE_ANTI_CLOCK_180)
		{	  
			CAM_TRACE("rotate anti-clock 180");
			mbm_media_aux_yuv42_rotate_180((int)g_mbm_camera_context_p->width, (int)g_mbm_camera_context_p->height, 
			(unsigned char *)g_mbm_camera_context_p->yuv420_data_buffer,
			(unsigned char *)g_mbm_camera_context_p->buff_addr2);

		}
	#endif
		else
		{			  
			CAM_TRACE("rotate normal");
		}		  
	}
	else
	{
		CAM_TRACE("only support yuv420");
		return MBM_CAMERA_FAIL;
	}
	
	if(g_mbm_camera_context_p->preview_rotate != MBM_CAMERA_PREVIEW_ROTATE_NORMAL)
	{
		kal_mem_cpy(g_mbm_camera_context_p->yuv420_data_buffer, (void*)g_mbm_camera_context_p->buff_addr2, g_mbm_camera_context_p->width * g_mbm_camera_context_p->height * 3/2);
	}
	
    return MBM_CAMERA_OK;
}

/**
  *  [PLT_REVIEW] return value MBM_CAMERA_OK is success
  */
Int32 mbm_camera_open(UInt16 width, UInt16 height)
{
    mbm_camera_init();
    
	if(MBM_CAMERA_OK != mbm_camera_power_on())
	{
		return MBM_CAMERA_FAIL;
	}
    /**
      *  [PLT_REVIEW] add if(MBM_CAMERA_OK == ret)
      */
	if(MBM_CAMERA_OK != mbm_camera_preview(width, height))
	{
		return MBM_CAMERA_FAIL;
	}

	return MBM_CAMERA_OK;
}

UInt32 mbm_camera_capture(UInt8* data_buf)
{
	MBM_ASSERT(data_buf != NULL, "mbm_camera_capture");
	
	CAM_TRACE("mbm_camera_capture");
	
	g_mbm_camera_context_p->yuv420_data_buffer = data_buf;

	return mbm_camera_data_req();
}

Int32 mbm_camera_close(void)
{
	if(MBM_CAMERA_OK != mbm_camera_preview_stop())
	{
		return MBM_CAMERA_FAIL;
	}
	
	return mbm_camera_power_off();
}

#if defined(__MMI_CAMERA__) || defined(__MMI_CAMCORDER__)

extern mdi_camera_context_struct *mdi_camera_p;

UInt16 mbm_get_system_camera_state(void)
{
    return mdi_camera_p->state;
}
#endif

#endif/* MBM_IMIBASE_SUPPORT */

